//
//  MeViewController.swift
//  Eternity
//
//  Created by ZhengWei on 2016/11/27.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class MeViewController: BaseViewController {
    
    // MARK: -  life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initBaseLayout()
        layoutPageSubviews()
        initNavItem(controller: self, imageName: "nav_comment_n", highlitedImageName: "nav_comment_h", selectedImageName: "", isLeft: false)
        initNavItem(controller: self, imageName: "nav_set_n", highlitedImageName: "nav_set_h", selectedImageName: "", isLeft: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - event response
    override func leftItemAction(sender: UIButton) {
        let settingVC = SettingViewController()
        settingVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(settingVC, animated: true)
    }
    
    override func rightItemAction(sender: UIButton) {
        let messageVC = MessageViewController()
        messageVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(messageVC, animated: true)
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.view.addSubview(meCollection)
    }
    
    func layoutPageSubviews() {
        meCollection.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
    }
    
    // MARK: - setters and getters
    var _layout: UICollectionViewFlowLayout!
    var layout: UICollectionViewFlowLayout {
        if _layout == nil {
            _layout = UICollectionViewFlowLayout()
        }
        return _layout
    }
    
    var _meCollection: UICollectionView!
    var meCollection: UICollectionView {
        if _meCollection == nil {
            _meCollection = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
            _meCollection.backgroundColor = UIColor(hexString: "#E5E5E5")
            _meCollection.delegate = self
            _meCollection.dataSource = self
            _meCollection.register(PersonalCenterHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "PersonalCenterHeader")
            _meCollection.register(NewHotAndFondCell.self, forCellWithReuseIdentifier: "NewHotAndFondCell")
        }
        return _meCollection
    }
    
}

extension MeViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let string = "NewHotAndFondCell"
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: string, for: indexPath) as! NewHotAndFondCell
        
//        cell.configUI()
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: SCREEN_WIDTH/2, height: 244)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: SCREEN_WIDTH, height: 239+(170/375*SCREEN_WIDTH))
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let string = "PersonalCenterHeader"
        
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: string, for: indexPath) as! PersonalCenterHeader
        header.myDelegate = self
        header.configUI()
        
        return header
    }
}

extension MeViewController: PersonalCenterHeaderDelegate {
    func setHeadAction(sender: UIButton) {
        let personalDataVC = PersonalDataViewController()
        personalDataVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(personalDataVC, animated: true)
    }
    
    func personalManagementAction(sender: UIButton) {
        switch sender.tag {
        case 300:
            let orderListVC = OrderListViewController()
            orderListVC.hidesBottomBarWhenPushed = true
            orderListVC.status = .WAITFORPAY
            self.navigationController?.pushViewController(orderListVC, animated: true)
            break
        case 301:
            let orderListVC = OrderListViewController()
            orderListVC.hidesBottomBarWhenPushed = true
            orderListVC.status = .WAITFORGOODS
            self.navigationController?.pushViewController(orderListVC, animated: true)
            break
        case 302:
            let afterSaleVC = AfterSaleViewController()
            afterSaleVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(afterSaleVC, animated: true)
            break
        case 303:
            let orderListVC = OrderListViewController()
            orderListVC.hidesBottomBarWhenPushed = true
            orderListVC.status = .ALLORDER
            self.navigationController?.pushViewController(orderListVC, animated: true)
            break
        case 304:
            let myPointsVC = MyPointsViewController()
            myPointsVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(myPointsVC, animated: true)
            break
        case 305:
            let collectionVC = CollectionViewController()
            collectionVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(collectionVC, animated: true)
            
            break
        case 306:
            let manageAdressVC = ManageAddressViewController()
            manageAdressVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(manageAdressVC, animated: true)
            break
        case 307:
            let serviceVC = ServiceViewController()
            serviceVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(serviceVC, animated: true)
            break
        default:
            break
        }
    }
}

